from Autotests.LocalMachine.Controls.Button import WebButton
from Autotests.LocalMachine.Controls.LineEdit import WebLineEdit
from Autotests.LocalMachine.Controls.ListView import WebListView, WebListViewDelegateBuilder
from Autotests.LocalMachine.Controls.ComboBox import WebComboBox
from Autotests.LocalMachine.Controls.BaseControl import WebFindKey
from Autotests.LocalMachine.Controls.CheckBox import WebHorizontalCheckBox
from Autotests.LocalMachine.Controls.Label import WebLabel
from Autotests.LocalMachine.Controls.BaseView import BaseWebViewLeafDelegate
from Autotests.LocalMachine.UiObjectsMap.AirClient import Options
from Autotests.LocalMachine.Pages.AirClient.BackupPage import BackupPlugins
from OptionsPage import OptionsPage


class DayOfWeek(object):
    daily = "127"
    monday = "1"
    tuesday = "2"
    wednesday = "4"
    thursday = "8"
    friday = "16"
    saturday = "32"
    sunday = "64"


class WebScheduleListDelegate(BaseWebViewLeafDelegate):
    def __init__(self, handler, index):
        super(WebScheduleListDelegate, self).__init__(handler, index)
        self._checkBoxEnabled = WebHorizontalCheckBox(self.waitChildControl(
            *Options.Schedule.listScheduleItem.checkboxEnabled))
        self._labelScheduleName = WebLabel(self.waitChildControl(*Options.Schedule.listScheduleItem.labelScheduleName))
        self._buttonEditSchedule = WebButton(self.waitChildControl(*Options.Schedule.listScheduleItem.btnEditSchedule))

    def setEnabled(self, enabled):
        self._checkBoxEnabled.setCheckState(enabled)

    def isEnabled(self):
        return self._checkBoxEnabled.isChecked()

    def scheduleName(self):
        return self._labelScheduleName.text()

    def showEditScheduleDialog(self):
        self._buttonEditSchedule.click()


class EditScheduleDialog(OptionsPage):
    def __init__(self):
        super(EditScheduleDialog, self).__init__()
        self._textBoxScheduleName = WebLineEdit(Options.Schedule.EditSchedule.txtboxScheduleName)
        self._textBoxFireTime = WebLineEdit(Options.Schedule.EditSchedule.txtboxFireTime)
        self._comboBoxPreBackup = WebComboBox(Options.Schedule.EditSchedule.cbPreBackup)
        self._listDaysOfWeek = WebListView(Options.Schedule.EditSchedule.listDaysOfWeek)
        self._listPlugins = WebListView(Options.Schedule.EditSchedule.listPlugins)
        self._comboBoxPostBackup = WebComboBox(Options.Schedule.EditSchedule.cbPostBackup)

    def goToHomeLocation(self):
        pass

    def clearSettings(self):
        self._listDaysOfWeek.deselectAll()
        self._listPlugins.deselectAll()
        defaultDataValue = '0'
        self._comboBoxPreBackup.selectItemsByDataValue(defaultDataValue)
        self._comboBoxPostBackup.selectItemsByDataValue(defaultDataValue)

    def save(self):
        WebComboBox(Options.Schedule.EditSchedule.btnSave).click()

    def cancel(self):
        WebComboBox(Options.Schedule.EditSchedule.btnCancel).click()

    def delete(self):
        WebButton(Options.Schedule.EditSchedule.btnDelete).click()

    def modifySchedule(self, scheduleName, fireTime, preBackupScript, dayOfWeek, plugin, postBackupScript):
        self._textBoxScheduleName.typeText(scheduleName)
        self._textBoxFireTime.typeText(fireTime)
        if preBackupScript:
            self._comboBoxPreBackup.selectItemsByText(preBackupScript)
        self._listDaysOfWeek.selectItemsByDataValue(dayOfWeek)
        self._listPlugins.selectItemsByDataValue(plugin)
        if postBackupScript:
            self._comboBoxPostBackup.selectItemsByText(postBackupScript)

    def getScheduleInfo(self):
        name = self._textBoxScheduleName.text()
        time = self._textBoxFireTime.text()
        pre = self._comboBoxPreBackup.currentItem().text
        day = self._listDaysOfWeek.selectedItems()[0].dataValue()
        plugin = self._listPlugins.selectedItems()[0].dataValue()
        post = self._comboBoxPostBackup.currentItem().text
        return (name, time, pre, day, plugin, post)


class ScheduleTab(OptionsPage):
    def __init__(self):
        super(ScheduleTab, self).__init__()
        self._buttonAddSchedule = WebButton(Options.Schedule.btnAddSchedule)
        shceduleListBuilder = WebListViewDelegateBuilder((WebFindKey.XPATH, 'li[@class="scheduleborder"]'),
                                                         WebScheduleListDelegate)
        self._listSchedule = WebListView(Options.Schedule.listViewSchedule, shceduleListBuilder)
        self._editScheduleDialog = EditScheduleDialog()

    def goToHomeLocation(self):
        super(ScheduleTab, self).goToHomeLocation()
        WebButton(Options.btnSchedule).click()

    @OptionsPage.getter
    def getScheduleCount(self):
        return self._listSchedule.rowsCount()

    def _getScheduleDelegate(self, scheduleName):
        items = self._listSchedule.itemsFor(lambda x: x.scheduleName() == scheduleName)
        return items[0] if items else None

    @OptionsPage.setter
    def addSchedule(self, scheduleName, fireTime="12:00", preBackupScript=None, dayOfWeek=DayOfWeek.monday,
                    plugin=BackupPlugins.filesystem, postBackupScript=None):
        self._buttonAddSchedule.click()
        edit = self._editScheduleDialog
        edit.modifySchedule(scheduleName, fireTime, preBackupScript, dayOfWeek, plugin, postBackupScript)
        edit.save()

    @OptionsPage.getter
    def getScheduleInfo(self, scheduleName):
        self._getScheduleDelegate(scheduleName).showEditScheduleDialog()
        info = self._editScheduleDialog.getScheduleInfo()
        self._editScheduleDialog.cancel()
        return info

    @OptionsPage.setter
    def editSchedule(self, oldScheduleName, newScheduleName, newFireTime, newPreBackupScript,
                     newDayOfWeek, newPlugin, newPostBackupScript):
        schedule = self._getScheduleDelegate(oldScheduleName)
        schedule.showEditScheduleDialog()
        edit = self._editScheduleDialog
        edit.clearSettings()
        edit.modifySchedule(newScheduleName, newFireTime, newPreBackupScript, newDayOfWeek,
                            newPlugin, newPostBackupScript)
        edit.save()

    @OptionsPage.setter
    def deleteSchedule(self, scheduleName):
        schedule = self._getScheduleDelegate(scheduleName)
        if schedule != None:
            schedule.showEditScheduleDialog()
            self._editScheduleDialog.delete()

    @OptionsPage.setter
    def setScheduleEnabled(self, schedule, enabled):
        self._listSchedule.transformItems(lambda x: x.scheduleName() == schedule, lambda x: x.setEnabled(enabled))

    @OptionsPage.getter
    def isScheduleEnabled(self, scheduleName):
        schedule = self._getScheduleDelegate(scheduleName)
        return schedule.isEnabled()

    @OptionsPage.getter
    def getScheduleList(self):
        return [item.shedule for item in self._listSchedule.items()]

    def viewEditSchedule(self, schedule=''):
        self.goToHomeLocation()
        if schedule == '':
            self._buttonAddSchedule.click()
        else:
            self._getScheduleDelegate(schedule).showEditScheduleDialog()
